﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Perceiveit.Xml.Converters
{
    public class BinaryXConverter : XConverter<byte[]>
    {

        public override byte[] FromString(string value)
        {
            byte[] data;
            if (string.IsNullOrEmpty(value))
                data = null;
            else
            {
                try
                {
                    data = Convert.FromBase64String(value);
                }
                catch (FormatException)
                {
                    data = null;
                }
            }
            return data;
        }

        public override string ToString(byte[] value)
        {
            if (null == value || value.Length == 0)
                return string.Empty;
            else
                return Convert.ToBase64String(value, Base64FormattingOptions.InsertLineBreaks);
        }
    }
}
